package model;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * 分页信息
 *
 * @author: Jacky Chai
 * @date: 2019/4/19
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageInfo extends BaseObject implements Serializable {

    private static final long serialVersionUID = -7802809046658158292L;
    /**
     * 页码
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "页码", required = true)
    private int pageNo = 1;

    /**
     * 页大小
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "页大小", required = true)
    private int pageSize = 20;

    /**
     * 总数
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "总数")
    private int totalCount;

    /**
     * 总页数
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "总页数")
    private int totalPage;

    public PageInfo() {
    }

    public PageInfo(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public PageInfo(int pageNo, int pageSize, int totalCount) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        computeTotalPage();
    }

    /**
     * 对分页结果进行计算, 之前需要正确设置 pageNo 和 pageSize
     */
    public void compute(int totalCount) {
        int totalPage = totalCount / pageSize;
        if (totalCount % pageSize != 0) {
            totalPage++;
        }
        this.totalCount = totalCount;
        this.totalPage = totalPage;
    }

    /**
     * 对分页结果进行计算, 之前需要正确设置 pageNo,pageSize和totalCount
     */
    private void computeTotalPage() {
        int totalPage = totalCount / pageSize;
        if (totalCount % pageSize != 0) {
            totalPage++;
        }
        this.totalPage = totalPage;
    }
}
